# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Milan P. Stanić <mps@arvanta.net>
# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=zig
pkgver=0.11.0
pkgrel=3
pkgdesc="General-purpose programming language designed for robustness, optimality, and maintainability"
url="https://ziglang.org/"
# arm*, x86: fails to build (allocation failed)
# ppc64le,riscv64: builds but the test command crashes
# s390x: blocked by lld-dev
arch="x86_64 aarch64"
license="MIT"
install="$pkgname.pre-upgrade"
_llvmver=16
makedepends="
	clang$_llvmver-dev
	cmake
	libxml2-dev
	lld$_llvmver-dev
	llvm$_llvmver-dev
	samurai
	zlib-dev
	"
provides="zig-dev=$pkgver-r$pkgrel"
subpackages="$pkgname-doc"
source="zig-$pkgver-1.tar.xz::https://ziglang.org/download/$pkgver/zig-$pkgver.tar.xz
	$pkgname-cbe-omit-trap-in-naked-fn-1.patch::https://github.com/ziglang/zig/commit/8c6a32ac28b9cccc9dd3f371c77458cac853b240.diff
	"

build() {
	local crossopts=
	if [ "$CBUILD" != "$CHOST" ]; then
		crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi

	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_PREFIX_PATH=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DZIG_VERSION="$pkgver" \
		-DZIG_SHARED_LLVM=ON \
		-DZIG_TARGET_MCPU=baseline \
		-DCLANG_INCLUDE_DIRS=/usr/lib/llvm$_llvmver/include \
		$crossopts

	cmake --build build

	./build/stage3/bin/zig build docs
}

check() {
	./build/stage3/bin/zig build test \
		-Dcpu=baseline \
		-Dskip-debug \
		-Dskip-release-safe \
		-Dskip-release-small \
		-Dskip-non-native
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	mkdir -p "$pkgdir/usr/share/doc/"
	cp -r zig-out/doc "$pkgdir/usr/share/doc/$pkgname"
}

sha512sums="
c19a8ae6d440a16dd5e77941fc77c036d1f284f1784376625e63b65f30b4acad62f9f4217bc818c401f88c2c33f8722f26fc467c3e23c29a89178c4b1882d1c4  zig-0.11.0-1.tar.xz
cac6500b4aa6eb2129d7df73315cbf487b05577a6d1a1e300bcc8a5e950fa00fb2143e26105b0348393df26edb6a0b23b60ffefe68fa7fcbad2bd4a597bd72a6  zig-cbe-omit-trap-in-naked-fn-1.patch
"
